﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BUS;
using DTO;
using TuyenDungViecLam.CommonResource;
namespace TuyenDungViecLam.User_control
{
    public partial class LOG_IN : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void LoginButton_Click(object sender, EventArgs e)
        {

        }

        //protected void LoginStatus1_LoggingOut(object sender, LoginCancelEventArgs e)
        //{
        //    Logout.Visible = false;
        //    Login.Visible = true;
        //    Session["Role"] = "";
        //    Session["UserID"] = "";
        //    Session["UserName"] = "";
        //}

        protected void Page_PreRender(object sender, EventArgs e)
        {
            if (Session["Role"].ToString() == "NguoiTimViec")
            {
                lbtaikhoan.Text = Session["UserName"].ToString();
                lbvaitro.Text = "Người tìm việc";
                Login.Visible = false;
                Logout.Visible = true;
            }
            else
            {
                Logout.Visible = false;
                Login.Visible = true;
            }
        }

        protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
        {

        }

        protected void LoginEmployee_Authenticate(object sender, AuthenticateEventArgs e)
        {
            if (Membership.ValidateUser(LoginEmployee.UserName, LoginEmployee.Password))
            {
                string User_ID = "";
                string Role = "";
                NguoiTimViec_BUS.GetRole(LoginEmployee.UserName, ref User_ID, ref Role);
                Session["Role"] = Role;
                Session["UserID"] = User_ID;
                Session["UserName"] = LoginEmployee.UserName;
                if (Session["Role"].ToString() == "NguoiTimViec")
                {
                    e.Authenticated = true;
                    if (Request.QueryString["Mcv"] != null)
                    {
                        string[] str = Request.QueryString["Mcv"].Split(',');
                        for (int i = 0; i < str.Length; i++)
                            NguoiTimViec_BUS.LuuCongViecDaXem(int.Parse(Session["UserID"].ToString()), int.Parse(str[i]), DateTime.Now);
                    }
                    if (Request.QueryString["Mct"] != null)
                    {
                        string Flag = "";
                        NguoiTimViec_BUS.NhanThongTinTuNhaTuyenDung(Session["UserID"].ToString(), Request.QueryString["Mct"], ref Flag);
                    }
                    Logout.Visible = true;
                    lbtaikhoan.Text = Session["UserName"].ToString();
                    lbvaitro.Text = "Người tìm việc";
                    Login.Visible = false;
                }
                if (Session["Role"].ToString() == "NhaTuyenDung")
                {
                    Session["Role"] = "";
                    Session["UserID"] = "";
                    Session["UserName"] = "";
                    LoginEmployee.FailureText = "Vui lòng đăng nhập ngoài trang chủ";
                }
                if (Session["Role"].ToString() == "Administrator")
                {
                    Session["Role"] = "";
                    Session["UserID"] = "";
                    Session["UserName"] = "";
                    LoginEmployee.FailureText = "Vui lòng đăng nhập ngoài trang chủ";
                }
            }
            else
            {
                LoginEmployee.FailureText = "Đăng nhập thất bại";
            }
        }

        protected void LoginStatus1_LoggingOut(object sender, LoginCancelEventArgs e)
        {
            Session["Role"] = "";
            Session["UserID"] = "";
            Session["UserName"] = "";
            e.Cancel = false;
        }
    }
}